define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ab/index' + location.search,
                    add_url: 'ab/add',
                    edit_url: 'ab/edit',
                    del_url: 'ab/del',
                    multi_url: 'ab/multi',
                    import_url: 'ab/import',
                    table: 'ab',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: __('Admin_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'promote_url', title: __('Promote_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'ip_limit', title: __('Ip_limit'), searchList: {"default":__('Ip_limit default'),"country":__('Ip_limit country')}, formatter: Table.api.formatter.normal},
                        {field: 'country_ids', title: __('Country_ids'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function (val, row) {
                            var arr = val.split(',');
                            var i=0, len=arr.length, txt = [];
                            for (i; i<len; i++) {
                                txt.push(Config.countryList[arr[i]]);
                            }
                            return txt.join(',');
                         }},
                        {field: 'device', title: __('Device'), operate: 'LIKE'},
                        {field: 'pixel', title: __('Pixel'), operate: 'LIKE'},
                        {field: 'event_name', title: __('Event_name'), searchList: {"Contact":__('Event_name contact')}, formatter: Table.api.formatter.normal},
                        {field: 'content_type', title: __('Content_type'), searchList: {"review":__('Content_type review'),"landing":__('Content_type landing')}, formatter: Table.api.formatter.normal},
                        {field: 'review_url', title: __('Review_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'landingpage_id', title: __('Landingpage_id')},
                        {field: 'material_type', title: __('Material_type'), searchList: {"1":__('Material_type 1'),"2":__('Material_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'desc', title: __('Desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'white_ip_list', title: __('White_ip_list'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
